﻿using BaseAuthRole.DTO.Auth;
using BaseAuthRole.IService;
using BaseAuthRole.Web.Filters;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RS.BaseModel.Common;
using RS.Environment;
using RS.Tool.Base;

namespace BaseAuthRole.Web.Controllers
{
    [Route("[controller]/[action]")]   
    [PageAuthCheckFilter]
    [RoleActionCheckFilter]
    public class AppManagerController : Controller
    {
        IApplicationService _applictationService;

        public AppManagerController(IApplicationService applictationService)
        {
            this._applictationService = applictationService;
        }

        public IActionResult Index()
        {
            return View();
        }

        public ActionResult AppDetail(string id)
        {

            var info = this._applictationService.GetApplicationModel(id);


            if (info == null)
            {
                return Redirect(ConfigurationHelper.GetAppSettings("noresource_url"));
            }
            else
            {
                ViewData["App"] = info;
            }

            return View();
        }

        public ActionResult AppEdit(string id)
        {

            if (!string.IsNullOrEmpty(id))
            {


                var info = this._applictationService.GetApplicationModel(id);

                if (info == null)
                {
                    return Redirect(ConfigurationHelper.GetAppSettings("noresource_url"));
                }
                else
                {
                    ViewData["App"] = info;
                }
            }

            return View();
        }

        #region

        public string GetApp(string para,int pagenum,int pagesize=10)
        {
            var result = new CommonResult();

            var condition = "";           

            long count = 0;

            var paraObj = new Dictionary<string, string>();

            if (!string.IsNullOrEmpty(para))
            {
               condition += " \"Name\" like @para";

                paraObj.Add("para", $"%{para}%");

            }

            var info = this._applictationService.GetApplicationModel(pagenum, pagesize, condition, " \"CreateTime\" desc", out count, paraObj);

            result.IsError = false;

            result.Result = new
            {

                content = info,

                num = Math.Ceiling(count / (decimal)pagesize)
            };

            return JsonConvert.SerializeObject(result);
        }

        [HttpPost]
        public string EditApp(string id,string name,string des,string auth_domain)
        {
            var result = new CommonResult();

            if (string.IsNullOrEmpty(id))
            {
                var app = new ApplicationModel
                {
                    Secret = SecretHelper.GetMD5(Guid.NewGuid().ToString("N")),
                    Name = name,
                    Description = des,
                    AuthDomain = auth_domain
                };

                var r = this._applictationService.AddApplication(app);

                if (r > 0)
                {
                    result.IsError = false;
                    result.Result = app.Id;
                }
                else
                {
                    result.Message = "操作失败";
                }
            }
            else
            {
                var app = this._applictationService.GetApplicationModel(id);

                if (app == null)
                {
                    result.Message = "无效的appid";
                    result.Value = RS.BaseModel.Enum.EnumBaseResult.DisabledApp;
                    return JsonConvert.SerializeObject(result);
                }
                else
                {
                    app.Name = name;
                    app.Description = des;
                    app.AuthDomain = auth_domain;

                    var r = this._applictationService.UpdateApplicationModel(app);
                    if (r > 0)
                    {
                        result.IsError = false;
                        result.Result = app.Id;

                    }
                    else
                    {
                        result.Message = "操作失败";
                    }
                }
            }
            return JsonConvert.SerializeObject(result);
        }

        [HttpPost]
        public string EditAppStatus(string id,int status)
        {

            var result = new CommonResult();

            var app = this._applictationService.GetApplicationModel(id);

            if (app == null)
            {
                result.Message = "无效的appid";
                result.Value = RS.BaseModel.Enum.EnumBaseResult.DisabledApp;
                return JsonConvert.SerializeObject(result);
            }

            app.Status = status;

            var r = this._applictationService.UpdateApplicationModel(app);

            result.IsError = false;

            return JsonConvert.SerializeObject(result);

        }


        #endregion


    }
}
